﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Data;

public partial class DMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // NavigateUrl= '<%= Session("url").ToString()%>'
        if (!Page.IsPostBack)
        {

            if (Session["user"] != null)
            {
                
                login.Visible = false;
                //logout.Visible = true;

            }
        }
       
            if (Session["user"] != null)
            {

                lk1.PostBackUrl = Session["url"].ToString();
            }
       

    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-SA");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ar-SA");
        Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;

        Session["CurrentCulture"] = "ar-SA";
        Server.Transfer(Request.Path);
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Session["CurrentCulture"] = "en-US";
        Server.Transfer(Request.Path);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dr = DB.Authenticate(username.Text, password.Text);

        if (dr.Rows.Count > 0)
        {

            DataRow dt = dr.Rows[0];
            string act = dt["active"].ToString();
            if (dt["active"].ToString() == "" || Boolean.Parse(dt["active"].ToString()))
            {
                Session["user"] = dt["username"].ToString() + ","+dt["id"].ToString();
                string co = dt["company_id"].ToString();
                Session["company_id"] = co;
                Session["role_id"] = dt["role_id"].ToString();

              

                switch (int.Parse(dt["role_id"].ToString()))
                {
                    case 1:
                        Session["url"] = "~/ManagerDefault.aspx";
                        lk1.PostBackUrl = "~/ManagerDefault.aspx";
                        Response.Redirect("~/ManagerDefault.aspx");

                        break;
                    case 2:
                        DataSet dss = DB.getdata("select endofsub from [subscriptions]   WHERE endofsub>=getdate() and [company_id] = " + dt["company_id"].ToString());
                       
                        if (Boolean.Parse(dt["active"].ToString())&&(dss.Tables[0].Rows.Count>0))
                 
                        {
                            Session["subend"] = dss.Tables[0].Rows[0][0].ToString(); 
                        Session["url"] = "~/companypart/CompanyNotifications.aspx";
                        lk1.PostBackUrl = "~/companypart/CompanyNotifications.aspx";
                        Response.Redirect("~/companypart/CompanyNotifications.aspx");
                        }
                        else
                        {
                            DB.UPDATE("companies", "int,id," + dt["company_id"].ToString(), "active", "int","0");//("g", culture));

                            res.Text = this.GetGlobalResourceObject("Resource", "stop").ToString();


                        }
                        break;
                    case 3:
                        DataSet dss2 = DB.getdata("select endofsub from [subscriptions]  WHERE endofsub>=getdate() and [company_id] = " + dt["company_id"].ToString());

                        if (Boolean.Parse(dt["active"].ToString()) && (dss2.Tables[0].Rows.Count > 0))
                        {
                            Session["subend"] = dss2.Tables[0].Rows[0][0].ToString(); 


                            Session["url"] = "~/followerpart/CompanyNotifications.aspx";
                            lk1.PostBackUrl = "~/followerpart/CompanyNotifications.aspx";
                            Response.Redirect("~/followerpart/CompanyNotifications.aspx");
                        }
                        else
                        {
                            DB.UPDATE("companies", "int,id," + dt["company_id"].ToString(), "active", "int", "0");//("g", culture));

                            res.Text = this.GetGlobalResourceObject("Resource", "stop").ToString();


                        }

                        break;
                    default:
                        Session["url"] = "~/Default.aspx";
                        Response.Redirect("~/Default.aspx");

                        break;

                }
            }
            else
            {
                res.Text = this.GetGlobalResourceObject("Resource", "stop").ToString();

            }


        }
        else
        {
            res.Text = this.GetGlobalResourceObject("Resource", "notcorrect").ToString();
        }

    }
    protected void LinkButton8_Click(object sender, EventArgs e)
    {
        string obj = Session["CurrentCulture"].ToString();

        Session.Clear();
        Session["CurrentCulture"] = obj;
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(obj);
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(obj);
        Response.Redirect("~/Default.aspx");
    }
}
